package com.yjt.admin.system.controller.org;


import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yjt.admin.system.constant.SysCons;
import com.yjt.admin.system.entity.Org;
import com.yjt.admin.system.form.OrgForm;
import com.yjt.admin.system.form.org.CompanyOrgForm;
import com.yjt.admin.system.param.org.CompanyOrgParam;
import com.yjt.admin.system.param.org.OrgParam;
import com.yjt.admin.system.service.OrgService;
import com.yjt.admin.system.vo.org.CompanyOrgVO;
import com.yjt.admin.system.vo.org.OrgVO;
import com.yjt.common.enums.ModulEnum;
import com.yjt.common.enums.OperTypeEnum;
import com.yjt.common.log.annotation.OperLog;
import com.yjt.common.utils.CopyObject;
import com.yjt.common.vo.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 组织表 前端控制器
 * </p>
 *
 * @author zzp
 * @since 2021-10-09
 */
@RefreshScope // 支持Nacos的动态刷新功能
@RestController
@RequestMapping("/admin/system/company-org")
@Api(tags = "公司管理")
public class CompanyOrgController {

    @Autowired
    private OrgService orgService;

    @PostMapping("/add")
    @ApiOperation(value = "增加公司信息", notes = "返回JSONObject")
    @OperLog(operModul = ModulEnum.ORG_MODUL, operType = OperTypeEnum.ADD, operDesc = "新增公司信息")
    public CommonResult addCompanyOrg(@RequestBody @Validated CompanyOrgForm companyOrgForm) {
        OrgForm orgForm = new OrgForm();
        BeanUtils.copyProperties(companyOrgForm, orgForm);
        orgForm.setOrgType(SysCons.ORG_TYPE[2]);
        orgService.addOrg(orgForm);
        return CommonResult.success("公司新增成功");
    }


    @PutMapping("/update")
    @ApiOperation(value = "修改公司信息", notes = "返回JSONObject")
    @OperLog(operModul = ModulEnum.ORG_MODUL, operType = OperTypeEnum.MOD, operDesc = "修改公司信息")
    public CommonResult updateCompanyOrg(@RequestBody @Validated CompanyOrgForm companyOrgForm) {
        OrgForm orgForm = new OrgForm();
        BeanUtils.copyProperties(companyOrgForm, orgForm);
        orgForm.setOrgType(SysCons.ORG_TYPE[2]);
        orgService.updateOrg(orgForm);
        return CommonResult.success("公司修改成功");
    }


    @GetMapping("/{orgId}")
    @ApiOperation(value = "查询公司信息", notes = "返回JSONObject")
    @ApiImplicitParam(name = "orgId", value = "公司id")
    public CommonResult<CompanyOrgVO> getOneCompanyOrg(@PathVariable String orgId) {
        OrgVO orgVO = orgService.getOneOrg(orgId);
        CompanyOrgVO companyOrgVO = new CompanyOrgVO();
        BeanUtils.copyProperties(orgVO, companyOrgVO);
        return CommonResult.success(companyOrgVO, "公司信息查询成功");
    }


    @SneakyThrows
    @PostMapping("/list-page")
    @ApiOperation(value = "查询公司分页信息", notes = "返回JSONObject")
    public CommonResult<IPage<CompanyOrgVO>> getPageCompanyOrg(@RequestBody CompanyOrgParam companyOrgParam) {
        //复制查询参数
        OrgParam orgParam = new OrgParam();
        BeanUtils.copyProperties(companyOrgParam, orgParam);
        IPage<OrgVO> page = orgService.getPageOrg(orgParam);
        //构造返回参数
        List<OrgVO> orgListVO = page.getRecords();
        List<CompanyOrgVO> companyOrgVOList = CopyObject.copyList(orgListVO, CompanyOrgVO.class);
        IPage<CompanyOrgVO> companyOrgVOPage = new Page();
        BeanUtils.copyProperties(page, companyOrgVOPage);
        companyOrgVOPage.setRecords(companyOrgVOList);
        return CommonResult.success(companyOrgVOPage, "查询公司分页信息成功");
    }

    @PutMapping("/forbid")
    @ApiOperation(value = "禁用公司", notes = "返回JSONObject")
    @OperLog(operModul = ModulEnum.ORG_MODUL, operType = OperTypeEnum.MOD, operDesc = "禁用公司")
    @ApiImplicitParam(name = "orgId", value = "公司id", dataType = "string")
    public CommonResult stopCompanyOrg(String orgId) {
        //构造参数
        LambdaUpdateWrapper<Org> lambdaUpdateWrapper = Wrappers.<Org>lambdaUpdate()
                .set(Org::getIsForbid, 0)
                .eq(Org::getOrgId, orgId);
        orgService.update(lambdaUpdateWrapper);
        return CommonResult.success("公司禁用成功");
    }

}
